package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.exception.CustomException;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private DishFlavorService dishFlavorService;

    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private SetmealService setmealService;


    @Override
    public void saveDishWithFlavors(DishDto dishDto) {
        // 1. 添加菜品信息
        this.save(dishDto);

        // 2. 添加口味信息
        // 2.1 获取口味信息
        List<DishFlavor> flavors = dishDto.getFlavors();
        // 2.2 当前口味信息中确实菜品id 添加菜品id
        flavors.forEach(dishFlavor -> {
            dishFlavor.setDishId(dishDto.getId());
        });

        // 批量添加口味信息
        dishFlavorService.saveBatch(flavors);
    }

    @Override
    public DishDto getByIdWithFlavors(Long id) {
        // 1. 查询基础的信息
        Dish dish = this.getById(id);

        // 2. 查询口味信息
        LambdaQueryWrapper<DishFlavor> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(DishFlavor::getDishId,id);

        List<DishFlavor> flavors = dishFlavorService.list(lambdaQueryWrapper);

        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish,dishDto);
        dishDto.setFlavors(flavors);

        return dishDto;
    }

    @Override
    public void updateDishWithFlavors(DishDto dishDto) {
        // 1. 修改基本数据
        this.updateById(dishDto);

        // 2. 修改口味信息
        // 2.1 先根据菜品id删除口味信息
        LambdaQueryWrapper<DishFlavor> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(DishFlavor::getDishId,dishDto.getId());
        dishFlavorService.remove(lambdaQueryWrapper);

        // 2.2 新增口味信息
        List<DishFlavor> flavors = dishDto.getFlavors();
        // 需要给口味添加菜品id
        flavors.forEach(flavor -> {
            flavor.setDishId(dishDto.getId());
        });

        dishFlavorService.saveBatch(flavors);
    }

    @Transactional
    @Override
    public void deleteDishWithFlavors(List<Long> ids) {
        // 1. 判断是否有起售的菜品
        for (Long id : ids) {
            // 根据id查询当前菜品信息
            Dish dish = this.getById(id);
            if (dish.getStatus().equals(1)) {
                throw new CustomException("删除的菜品正在售卖，不能删除");
            }
        }

        // 2. 是否关联的有套餐
        for (Long id : ids) {
            LambdaQueryWrapper<SetmealDish> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(SetmealDish::getDishId, id);
            int count = setmealDishService.count(lambdaQueryWrapper);
            if (count > 0) {
                throw new CustomException("菜品中有关联的套餐，不能直接删除");
            }
        }

        // 3. 先删除菜品对应的口味信息
        LambdaQueryWrapper<DishFlavor> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(DishFlavor::getDishId, ids);
        dishFlavorService.remove(lambdaQueryWrapper);

        // 4. 删除菜品信息
        this.removeByIds(ids);
    }

    @Override
    public void updateByStatus(Integer status, List<Long> ids) {
        // 1. 停售操作时，判断当前菜品是否关联的有起售套餐
        if (status.equals(0)) {

            // 根据菜品id查询  套餐菜品表的数据，获取套餐id
            LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();
            setmealDishLambdaQueryWrapper.in(SetmealDish::getDishId,ids);
            List<SetmealDish> setmealDishList = setmealDishService.list(setmealDishLambdaQueryWrapper);

            // 判断菜品是否关联的有套餐
            if (CollectionUtils.isNotEmpty(setmealDishList)) {

                // 获取所有的套餐Id  setmealId
                List<Long> setmeals = setmealDishList.stream().map(setmealDish -> setmealDish.getSetmealId()).collect(Collectors.toList());

                // 根据套餐id 查询套餐信息
                LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
                setmealLambdaQueryWrapper.in(Setmeal::getId, setmeals);
                setmealLambdaQueryWrapper.eq(Setmeal::getStatus, 1);

                int count = setmealService.count(setmealLambdaQueryWrapper);

                if (count > 0) {
                    throw new CustomException("菜品有关联的在售套餐，不能直接删除!");
                }
            }

        }

        // 2. 更新菜品的状态信息
        // 下面这种方法，只能修改状态，并不会走元数据，不会自动修改修改时间和修改人
        LambdaUpdateWrapper<Dish> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.in(Dish::getId, ids);
        lambdaUpdateWrapper.set(Dish::getStatus, status);

        this.update(new Dish(),lambdaUpdateWrapper);

    }
}
